package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.index.entity.Blog;
import com.kun.index.mapper.BlogMapper;
import com.kun.index.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 博客表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-02-28
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    private BlogMapper blogMapper;


    @Override
    public Map<String,Object> queryBlogs(Map map) {
        HashMap<String, Object> resultMap = new HashMap<>();
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if (map.get("queryTitleLike")!=null){
            wrapper.like("title",(String)map.get("queryTitleLike"));
        }
        if (map.get("pageIndex")!=null&&map.get("pageSize")!=null){
            int pageIndex = (Integer) map.get("pageIndex");
            int pageSize = (Integer) map.get("pageSize");

            Page<Blog> page = new Page<>(pageIndex,pageSize);
            blogMapper.selectPage(page, wrapper);
            long total =  page.getTotal();
            List<Blog> blogs = page.getRecords();
            resultMap.put("totalBlogNum",total);
            resultMap.put("blogs",blogs);
        }else {
            List<Blog> blogs = blogMapper.selectList(wrapper);
            resultMap.put("blogs",blogs);
        }
        return resultMap;
    }

    @Override
    public int addBlog(Blog blog) {

        return blogMapper.insert(blog);
    }

    @Override
    public int updateBlog(Blog blog) {
        return blogMapper.updateById(blog);
    }

    @Override
    public int deleteBlog(Blog blog) {
        return blogMapper.deleteById(blog.getId());
    }

    @Override
    public int batchDeleteBlog(List<Integer> ids) {
        return blogMapper.deleteBatchIds(ids);
    }

    @Override
    public Blog queryBlogById(Integer id) {

        return blogMapper.selectById(id);
    }
}
